<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
// 获取资源文件
use think\Db;



/**
 * @title 面包屑导航
 * @id 商品分类的id
 * return 面包屑数组
 */
function help_breadcrumb_goods($id) {

    $path = db('goods_cat')->where('id', $id)->value('path');

    $ids_arr = explode('-', $path);

    $breadcrumb_arr = [];

    foreach ($ids_arr as $value) {

        $breadcrumb_arr[$value] = db('goods_cat')->where('id', $value)->value('title');
    }

    $breadcrumb_arr = array_filter($breadcrumb_arr);

    return $breadcrumb_arr;
}

/**
 * @title 面包屑导航
 * @id 文章分类的id
 * return 面包屑数组
 */
function help_breadcrumb_article($id) {

    $path = db('article_cat')->where('id', $id)->value('path');

    $ids_arr = explode('-', $path);

    $breadcrumb_arr = [];

    foreach ($ids_arr as $value) {

        $breadcrumb_arr[$value] = db('article_cat')->where('id', $value)->value('title');
    }

    $breadcrumb_arr = array_filter($breadcrumb_arr);

    return $breadcrumb_arr;
}

/**
 * @title 通过alias的值获取导航列表
 */
function helper_nav_alias($alias, $tag = 'field:*;limit:99;order:listorder asc,id desc') {

    $tag = _sys_parm_array($tag);
    $field = !empty($tag['field']) ? $tag['field'] : '*';
    $limit = !empty($tag['limit']) ? $tag['limit'] : '';
    $order = !empty($tag['order']) ? $tag['order'] : 'listorder asc,id desc';


    $lists = Db::name('nav')->where('cid', 'IN', function($query) use($alias) {
                $query->name('nav_cat')->where('alias', $alias)->field('id');
            })
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();

    return $lists;
}


/**
 * @title 通过path的值获取文章列表
 */
function helper_article_path($path, $tag = 'field:*;limit:99;order:listorder asc,id desc') {

    $tag = _sys_parm_array($tag);
    $field = !empty($tag['field']) ? $tag['field'] : '*';
    $limit = !empty($tag['limit']) ? $tag['limit'] : '';
    $order = !empty($tag['order']) ? $tag['order'] : 'listorder asc,id desc';


    $lists = Db::name('article')->where('cid', 'IN', function($query) use($path) {
                $query->name('article_cat')->where('path', 'like', $path.'%')->field('id');
            })
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();

    return $lists;
}



/**
 * @title 文章
 * @param type $tag
 * @param type $wheres
 * @param type $single
 */
function helper_article($tag, $wheres = [], $single = false) {

    $tag = _sys_parm_array($tag);
    $field = !empty($tag['field']) ? $tag['field'] : '*';
    $limit = !empty($tag['limit']) ? $tag['limit'] : '';
    $order = !empty($tag['order']) ? $tag['order'] : 'id';

    if ($single) {
        return db('article')->field($field)->where($wheres)->order($order)->limit($limit)->find();
    } else {
        return db('article')->field($field)->where($wheres)->order($order)->limit($limit)->select();
    }
}

/**
 * @title 商品
 * @param type $tag
 * @param type $wheres
 * @param type $single
 * @return type
 */
function helper_goods($tag, $wheres = [], $single = false) {

    $tag = _sys_parm_array($tag);
    $field = !empty($tag['field']) ? $tag['field'] : '*';
    $limit = !empty($tag['limit']) ? $tag['limit'] : '';
    $order = !empty($tag['order']) ? $tag['order'] : 'id';

    if ($single) {
        return db('goods')->field($field)->where($wheres)->order($order)->limit($limit)->find();
    } else {
        return db('goods')->field($field)->where($wheres)->order($order)->limit($limit)->select();
    }
}

/**
 * @title 获取导航数据
 * $tag 查询标签，以字符串方式传入,例："field:post_title,post_content;limit:0,8;order:post_date desc,listorder desc;"
 * $wheres  专业的查询条件，如果对tp5的查询方法不熟练，建议不要使用此参数
 * $single 查询 列表 or 单条数据 
 */
function helper_nav($tag, $wheres = [], $single = false) {

    $tag = _sys_parm_array($tag);
    $field = !empty($tag['field']) ? $tag['field'] : '*';
    $limit = !empty($tag['limit']) ? $tag['limit'] : '';
    $order = !empty($tag['order']) ? $tag['order'] : 'id';

    if ($single) {
        return db('nav')->field($field)->where($wheres)->order($order)->limit($limit)->find();
    } else {
        return db('nav')->field($field)->where($wheres)->order($order)->limit($limit)->select();
    }
}

/**
 * @title 生成参数列表,以数组形式返回
 */
function _sys_parm_array($tag = '') {
    $param = array();
    $array = explode(';', $tag);
    foreach ($array as $v) {
        $v = trim($v);
        if (!empty($v)) {
            list($key, $val) = explode(':', $v);
            $param[trim($key)] = trim($val);
        }
    }
    return $param;
}
